package crane.bomb;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

/**
 * 
 * @author crane
 *
 */
public class BombScoreFrame extends MainFrame implements TableModelListener{

    /**
     * Constructor
     *
     */
    public BombScoreFrame(){}    

    /*
     * mBean
     */
    private MainBean mBean;
    
    /**
     * @param mBean
     */
    void setMainBean(MainBean mBean) {
        this.mBean = mBean;        
    }
    
    /**
     * @return mBean
     */
    MainBean getMainBean() {
        return this.mBean;
    }
    
    /**
     * DEFAULT_ROW
     */
    private static final int DEFAULT_ROW = 10;
    
    /**
     * SCORE_DATA
     */
    private static String[][] SCORE_DATA = null;
    
    /**
     * Button Action
     */
    public void actionPerformed(ActionEvent e) {
        
        Object obj = e.getSource();

        if (obj instanceof JButton) {
            JButton button = (JButton) obj;
            if ("RT_Game".equals(button.getName())){
                new BombAction().actStartGame(mBean);
            }
        }
    }
   
    /**
     * init Score Table
     *
     */
    protected void initFrame(JFrame frame){
        
        frame.setTitle("Player's Score");
        JTable table = new JTable(initTable());
        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        table.setAutoscrolls(true);
        table.setVisible(true);
        
        JScrollPane jsPane = new JScrollPane(table);
        
        // init Button
        JPanel bPanel = initButton();
        
        Container pane = frame.getContentPane();
        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
        pane.add(jsPane);
        pane.add(Box.createVerticalStrut(20));
        pane.add(bPanel);
        
        frame.pack();
        frame.setBounds(300, 190, 300, 290);
        frame.setVisible(true);
        frame.addWindowListener(this);
        
    }
    
    private JPanel initButton(){
        JPanel panel = new JPanel(); 
        JButton retGameBtn = new JButton();
        retGameBtn.setName("RT_Game");
        retGameBtn.setText("Return to Game");
        retGameBtn.addActionListener(this);
        panel.add(retGameBtn);
        return panel;
    }
    
    private AbstractTableModel initTable(){
        
        final String[] header = 
        {
                "Level",
                "Player",
                "Score"
        };
        
        XMLUtil xmlUtil = new XMLUtil();
        final List scoreList = xmlUtil.readXML(null);
        int size = scoreList.size();
        int rows = size;
        if (size < DEFAULT_ROW){
            rows = DEFAULT_ROW;
        }
        SCORE_DATA = new String[rows][3];
        for (int i = 0; i < size; i++) {
            SCORE_DATA[i] = (String[])scoreList.get(i);
        }
        if (size < DEFAULT_ROW){
            for (int i = 0; i < DEFAULT_ROW - size; i++) {
                SCORE_DATA[size + i] = new String[3];
            }
        }
        
        AbstractTableModel table = new AbstractTableModel(){
            
            /**
             * serialVersionUID
             */
            private static final long serialVersionUID = -9139570112827059881L;
            
            String[][] data = SCORE_DATA;
            
            public int getColumnCount() {

                return data[0].length;
            }

            public int getRowCount() {

                return data.length;
            }

            public Object getValueAt(int rowIndex, int columnIndex) {

                return data[rowIndex][columnIndex];
            }
            
            public String getColumnName(int colIdx){
                return header[colIdx];
            }
            
        };
        table.addTableModelListener(this);
        return table;
    }

    public void tableChanged(TableModelEvent tme) {
        int curRow = tme.getFirstRow();
        new BombAction().showModallog(mBean.getFrame(), "Message", SCORE_DATA[curRow][2]);        
        
    }
}
